using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BearUtil.Hidden;
using Sirenix.OdinInspector;
using UnityEngine;
using XNode;

namespace BearUtil
{
    [Node.NodeWidthAttribute(100), CreateNodeMenu(menuName: "特殊节点/合并"), Title("合并"), NodeTintAttribute("#548B54")]
    public class MergeNode : Node
    {
        [Input(connectionType = ConnectionType.Multiple, 
            backingValue = ShowBackingValue.Never, 
            typeConstraint = TypeConstraint.Inherited)]
        public Node pre;

        [Output(connectionType =  ConnectionType.Override, 
            typeConstraint = TypeConstraint.Inherited)]
        public Node next;

        
        
        public List<BearNode> GetLasts()
        {
            List<BearNode> nodes = new List<BearNode>();
            foreach (var nodePort in Inputs.First().GetConnections())
            {
                var bn = nodePort.node as BearNode;
                if (bn != null)
                    nodes.Add(bn);

                var mn = nodePort.node as MergeNode;
                if (mn != null)
                    nodes = nodes.Concat(mn.GetLasts()).ToList();
            }
            
            return nodes;
        }
        
        public BearNode GetNext()
        {
            var nodePort = Outputs.First().GetConnections().First();
            var bn = nodePort.node as BearNode;
            if (bn != null)
                return bn;

            var mn = nodePort.node as MergeNode;
            if (mn != null)
                return mn.GetNext();
            
            return null;
        }
    }
}